﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace StudentStatistics
{
    /// <summary>
    /// StudentAccuracyControl.xaml 的交互逻辑
    /// </summary>
    public partial class StudentAccuracyControl : UserControl
    {

        public Double RightRate
        {
            get { return (Double)GetValue(RightRateProperty); }
            set { SetValue(RightRateProperty, value); }
        }
        public static readonly DependencyProperty RightRateProperty =
            DependencyProperty.Register("RightRate", typeof(Double), typeof(StudentAccuracyControl), new PropertyMetadata(0.4d));

        public ObservableCollection<Info> Items
        {
            get { return (ObservableCollection<Info>)GetValue(ItemsProperty); }
            set { SetValue(ItemsProperty, value); }
        }
        public static readonly DependencyProperty ItemsProperty =
            DependencyProperty.Register("Items", typeof(ObservableCollection<Info>), typeof(StudentAccuracyControl), new PropertyMetadata(new ObservableCollection<Info>()));

        public ObservableCollection<Info> OrderIteams = new ObservableCollection<Info>();

        int a = 30;

        void StudentAccuracyControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                this.TxtTime.Fill = new AccuracyItemControl().ViewBrush;
                SetTxtContent(130);
                SetView(0);
            }
        }

        public StudentAccuracyControl()
        {
            InitializeComponent();

            Random r = new Random();
            for (int i = 0; i < a; i++)
            {
                OrderIteams.Add(new Info() { Value1 = (float)r.NextDouble() });
            }

            this.IsVisibleChanged += StudentAccuracyControl_IsVisibleChanged;
        }

        /// <summary>
        /// 当前页数
        /// </summary>
        private int CurrentPage = 0;
        /// <summary>
        /// 每页显示多少数据
        /// </summary>
        public int PageCount = 10;

        AminationHelpers amination = new AminationHelpers();

        private void NextBtn_Click(object sender, RoutedEventArgs e)
        {
            this.FrontBtn.IsEnabled = true;

            if ((CurrentPage + 1) * PageCount < OrderIteams.Count)
            {
                CurrentPage++;
            }

            if ((CurrentPage + 1) * PageCount >= OrderIteams.Count)
            {
                this.NextBtn.IsEnabled = false;
            }

            SetView(CurrentPage);
        }

        private void FrontBtn_Click(object sender, RoutedEventArgs e)
        {
            this.NextBtn.IsEnabled = true;
            if (CurrentPage > 0)
            {
                CurrentPage--;
            }

            if (CurrentPage == 0)
            {
                this.FrontBtn.IsEnabled = false;
            }
            SetView(CurrentPage);
        }

        private void SetView(int currentpage)
        {
            questionnumbercontrol.QuestionButtonIteams.Clear();
            int first = (currentpage) * PageCount;
            for (int i = 1; i <= PageCount; i++)
            {
                questionnumbercontrol.QuestionButtonIteams.Add((i + first).ToString());
            }
            var viewitems = new ObservableCollection<Info>(OrderIteams.Skip(currentpage * PageCount).Take(PageCount).ToList());
            amination.SetViewAnimation(viewitems, Items);
        }
        /// <summary>
        /// 设置柱状图对应的数据
        /// </summary>
        /// <param name="AnswerTime"></param>
        public void SetTxtContent(int AnswerTime)
        {
            if (AnswerTime % 5 == 0)
            {
                int AverageCount = AnswerTime / 5;
                this.Txt2.Text = AverageCount.ToString();
                this.Txt3.Text = (AverageCount * 2).ToString();
                this.Txt4.Text = (AverageCount * 3).ToString();
                this.Txt5.Text = (AverageCount * 4).ToString();
                this.Txt6.Text = AnswerTime.ToString();
            }
            else
            {
                if (true)
                {
                    AnswerTime++;
                    if (AnswerTime % 5 == 0)
                    {
                        int AverageCount = AnswerTime / 5;
                        this.Txt2.Text = AverageCount.ToString();
                        this.Txt3.Text = (AverageCount * 2).ToString();
                        this.Txt4.Text = (AverageCount * 3).ToString();
                        this.Txt5.Text = (AverageCount * 4).ToString();
                        this.Txt6.Text = AnswerTime.ToString();
                        return;
                    }
                }
            }
        }
    }
    public class Info : INotifyPropertyChanged
    {
        public float Value1
        {
            get;
            set;
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}
